package cn.itcast.Rainbow_self_mention.Activity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import cn.itcast.Rainbow_self_mention.R;
import cn.itcast.Rainbow_self_mention.Dao.SqlHelper;

public class History extends AppCompatActivity {
    TextView NUMBER,
            NAME_OF_SUBMIT,
            PHONE_OF_SUBMIT,
            TIME_OF_ORDER,
            TIME_OF_SUBMIT,
            NAME_OF_RECEIVER,
            PHONE_OF_RECEIVER,
            TIME_OF_RECEIVER,
            LOCATION,
            TEMPERATURE,
            NAME_OF_PRODUCTION;
    SqlHelper hp;
    String number;
    ImageView photo,back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        number=getIntent().getStringExtra("number");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foot_history);
    }


    @Override
    protected void onResume() {
        super.onResume();
        init();
        try {
            initdate();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void init(){
        NUMBER=findViewById(R.id.history_number);
        NAME_OF_SUBMIT=findViewById(R.id.history_putpeople);
        PHONE_OF_SUBMIT=findViewById(R.id.history_putpeolputnumber);
        TIME_OF_ORDER=findViewById(R.id.history_ordertime);
        TIME_OF_SUBMIT=findViewById(R.id.history_puttime);
        NAME_OF_RECEIVER=findViewById(R.id.history_takepeople);
        PHONE_OF_RECEIVER=findViewById(R.id.history_takenumber);
        TIME_OF_RECEIVER=findViewById(R.id.history_taketime);
        LOCATION=findViewById(R.id.history_address);
        NAME_OF_PRODUCTION=findViewById(R.id.history_name_of_good);
        back=findViewById(R.id.history_back);
        TEMPERATURE=findViewById(R.id.TEMPERATURE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        photo=findViewById(R.id.image);
        hp=new SqlHelper(this);

    }
    public void finish(){
        super.finish();
        overridePendingTransition(0, 0);
    }
    public void initdate() throws FileNotFoundException {
        SQLiteDatabase db=hp.getReadableDatabase();
        Cursor cursor=db.query("HISTORY",null,"NUMBER=?",new String[]{number},null, null,null);
        cursor.moveToFirst();
        NUMBER.setText(cursor.getString(cursor.getColumnIndex("NUMBER")));
        NAME_OF_SUBMIT.setText(cursor.getString(cursor.getColumnIndex("NAME_OF_SUBMIT")));
        PHONE_OF_SUBMIT.setText(cursor.getString(cursor.getColumnIndex("PHONE_OF_SUBMIT")));
        TIME_OF_ORDER.setText(cursor.getString(cursor.getColumnIndex("TIME_OF_ORDER")));
        TIME_OF_SUBMIT.setText(cursor.getString(cursor.getColumnIndex("TIME_OF_SUBMIT")));
        NAME_OF_RECEIVER.setText(cursor.getString(cursor.getColumnIndex("NAME_OF_RECEIVER")));
        PHONE_OF_RECEIVER.setText(cursor.getString(cursor.getColumnIndex("PHONE_OF_RECEIVER")));
        TIME_OF_RECEIVER.setText(cursor.getString(cursor.getColumnIndex("TIME_OF_RECEIVER")));
        TEMPERATURE.setText(cursor.getString(cursor.getColumnIndex("TEMPERATURE"))+"℃");
        LOCATION.setText(cursor.getString(cursor.getColumnIndex("LOCATION")));
        NAME_OF_PRODUCTION.setText(cursor.getString(cursor.getColumnIndex("NAME_OF_PRODUCTION")));
        String root = Environment.getExternalStorageDirectory().getAbsolutePath();
        String dirName = "MTZTG";
        File appDir = new File(root, dirName);
        String fileName = cursor.getString(cursor.getColumnIndex("NAME_OF_PICTURE"));
        File file = new File(appDir, fileName);
        if(file.exists()){
            Uri imageuri = FileProvider.getUriForFile(this, "Rainbow_self_mention", file);
            InputStream input = getContentResolver().openInputStream(imageuri);
            Bitmap bitmap = BitmapFactory.decodeStream(input);
            photo.setImageBitmap(bitmap);}
        db.close();
        cursor.close();

    }

}
